import axios from 'axios'
import {
  Message
} from 'element-ui'
// axios默认配置
// axios.defaults.timeout = 10000; // 超时时间
// axios.defaults.baseURL = 'http://127.0.0.1:3000'; // 默认地址
axios.defaults.baseURL = 'http://82.156.208.120:3000'; // 默认地址

// http request 拦截器
axios.interceptors.request.use(
  config => {
    if (config.url.indexOf('api') != -1) {
      if (window.sessionStorage.getItem('token')) {
        config.headers.token = window.sessionStorage.getItem('token');
      } else {
        Message({
          message: "请先登录",
          type: 'warning'
        });
      }
    } else {
    }
    return config;
  },
  error => {
    return Promise.reject(error.response);
  });

// 路由响应拦截
// http response 拦截器
axios.interceptors.response.use(
  response => {
    if (response.data.code == "401") {
      var that = this;
      Message({
        message: response.data.message,
        type: 'warning'
      });
      setTimeout(() => {
        window.sessionStorage.clear();
        window.location.href = '/'
      }, 1500);
      return
    } else {
      return response;
    }
  },
  error => {
    return Promise.reject(error.response)   // 返回接口返回的错误信息
  });



export default axios;
